package com.gw.quotes.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.gw.quotes.R;
import com.gw.quotes.entity.business.ProjectListEntity.ProjectItem;
import com.gw.quotes.entity.business.ProjectListEntity.ProjectItem.ProjectGood;
import com.gw.quotes.entity.business.ProjectListEntity.ProjectItem.ProjectGood.Projectaccessorie;
import com.gw.quotes.entity.business.ProjectListEntity.ProjectItem.ProjectGood.Projectoptional;
import com.gw.quotes.net.BaseServiceInter;
import com.gw.quotes.net.HttpCallBack;
import com.gw.quotes.net.Request;
import com.gw.quotes.net.Response;
import com.gw.quotes.net.service.GetMapInfoImpl;
import com.gw.quotes.util.CommonTools;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.yxw.view.NoScrollListView;
import com.yxw.view.adapter.ImageAdapter;

public class QuoteProductDetailActivity extends BaseActivity {

	// UI
	@ViewInject(id = R.id.listview_quote_optional)
	private NoScrollListView listview_quote_optional;
	@ViewInject(id = R.id.listview_quote_base)
	private NoScrollListView listview_quote_base;
	@ViewInject(id = R.id.img_product)
	private ImageView img_product;
	@ViewInject(id = R.id.text_price)
	private TextView text_price;
//	@ViewInject(id = R.id.text_old_price)
//	private TextView text_old_price;
	
	@ViewInject(id = R.id.text_goods_name)
	private TextView text_goods_name;
	
	

	@ViewInject(id = R.id.back_img, click = "back")
	private ImageView back_img;
	
	// 适配器
	private SimpleAdapter adapterProjectoptional;
	private ImageAdapter adapterProjectaccessorie;

	// 数据
	private List<Map<String, Object>> dataProjectoptional;
	private List<Map<String, Object>> dataProjectaccessorie;
	private ProjectItem projectItem;
	private ProjectGood projectGood;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_quote_product_detail);
		getIntentData();
		initData();
		initView();
		requestImages();
	}

	private void initView() {
//		imageLoader.displayImage(projectGood.getImgurl(), img_product, options);

		imageLoader.displayImage(projectGood.getImgurl(), img_product, options,
				new SimpleImageLoadingListener() {
					@Override
					public void onLoadingComplete(final String imageUri,
							View view, Bitmap loadedImage) {
						view.setOnClickListener(new View.OnClickListener() {
							@Override
							public void onClick(View v) {
								Intent intent = new Intent();
								intent.setClass(
										QuoteProductDetailActivity.this,
										ViewImageActivity.class);
								intent.putExtra("url", imageUri);
								startActivity(intent);
							}
						});
					}
				});
		text_price.setText(String.format("￥%.2f",projectGood.getAgreementprice()));// 协议价
		// text_old_price.setText(String.format("￥%.2f",projectGood.getAgreementprice()));// 原价
		text_goods_name.setText(projectGood.getGoodsname());
	}

	private void initData() {
		if (projectItem.getProjectGoodList() != null
				&& projectItem.getProjectGoodList().size() > 0) {
			projectGood = projectItem.getProjectGoodList().get(0);

			dataProjectoptional = new ArrayList<Map<String, Object>>();
			for (int i = 0; i < projectGood.getProjectoptionalList().size(); i++) {
				Projectoptional projectoptional = projectGood
						.getProjectoptionalList().get(i);
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("name", projectoptional.getGoodsoptionalname());
				map.put("price",
						String.format("￥%.2f",
								projectoptional.getRelativeprice()));
				dataProjectoptional.add(map);
			}
			adapterProjectoptional = new SimpleAdapter(this,
					dataProjectoptional, R.layout.item_quote_product_optional,
					new String[] { "name", "price" }, new int[] {
							R.id.text_name, R.id.text_price });
			listview_quote_optional.setAdapter(adapterProjectoptional);

			dataProjectaccessorie = new ArrayList<Map<String, Object>>();
			for (int i = 0; i < projectGood.getProjectaccessorieList().size(); i++) {
				Projectaccessorie projectaccessorie = projectGood
						.getProjectaccessorieList().get(i);
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("image", projectaccessorie.getImgurl());
				map.put("name", projectaccessorie.getGoodsname());
				map.put("price",
						String.format("￥%.2f",
								projectaccessorie.getAgreementprice()));
				dataProjectaccessorie.add(map);
			}
			adapterProjectaccessorie = new ImageAdapter(this,
					dataProjectaccessorie, R.layout.item_quote_product_base,
					new String[] { "image", "name", "price" }, new int[] {
							R.id.img_photo, R.id.text_name, R.id.text_price });
			listview_quote_base.setAdapter(adapterProjectaccessorie);
		}
	}
	
	public void requestImages() {
		dialog.show();
		BaseServiceInter httpService = new GetMapInfoImpl("getImageListByGoodsguid");
		Request request = new Request();
		if (projectGood!=null) {
			request.setParas("{'goodsguid':'" +projectGood.getGoodsguid()+"'}");
		}
		Response response = httpService.handler(request,
				new HttpCallBack<Response<HashMap<String, Object>>>() {
					@Override
					public void onFinished(
							Response<HashMap<String, Object>> response) {
						if (response.getResult() == Response.RESULT_SUCCESS) {
							// Toast.makeText(LoginActivity.this,response.getMsg(),Toast.LENGTH_SHORT).show();
							
							// CommonTools.showShortToast(QuoteProductDetailActivity.this,response.getObj().toString());
						} else {
							CommonTools.showShortToast(QuoteProductDetailActivity.this, response.getMsg());
							// Toast.makeText(LoginActivity.this,response.getMsg(),Toast.LENGTH_SHORT).show();
						}
						dialog.dismiss();
					}
				});
	}

	public void back(View v) {
		finish();
	}
	
	private void getIntentData() {
		Intent intent = getIntent();
		projectItem = (ProjectItem) intent.getSerializableExtra("itemInfo");
	}

}
